package com.zlb.app.common.config;

import com.zlb.app.common.interceptor.AuthInterceptor;
import com.zlb.app.common.interceptor.PaginationInterceptor;
import com.zlb.app.common.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Desc
 * @Author zWX1367238
 * @Date 2024-08-14 15:12:00
 **/
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private SecurityProperties securityProperties;

    // 自定义分页器
    @Autowired
    private PaginationInterceptor paginationInterceptor;

    @Autowired
    private AuthInterceptor authInterceptor;

    /**
     * 添加拦截器,处理分页信息
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(paginationInterceptor);
        registry.addInterceptor(authInterceptor);
        // 路由拦截鉴权
        // registry.addInterceptor(new SaInterceptor(handle -> {
        //     // 除/auth/login外全部做登录校验
        //     SaRouter.match("/**").notMatch(securityProperties.getWhiteUrls()).check(r -> StpUtil.checkLogin());
        // })).addPathPatterns("/**");
    }

    // 允许跨越
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                // 是否发送cookie
                .allowCredentials(true)
                // 放行哪些原始头部信息
                .allowedHeaders("*")
                // 放行哪些匹配上的原始域
                .allowedOriginPatterns("*")
                // 放行哪些请求方式
                .allowedMethods("*");
    }

}
